package com.hpe.util;

/**
 * Created by 李宇寒
 * 2017/9/25 0025 上午 9:23.
 */

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class DownLoadExcel {

    public void downExcel(List<String> list,String[] title,OutputStream out){
        //创建Excel工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个工作表sheet
        HSSFSheet sheet = workbook.createSheet();
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell =null;

        for(int i =0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        //追加数据
        for(int i = 1;i<=list.size();i++){
            HSSFRow nextrow = sheet.createRow(i);
            String values = list.get(i-1);
            String[] cells = values.split(",");
            for(int j =0;j<cells.length;j++){
                cell = nextrow.createCell(j);
                cell.setCellValue(cells[j]);
            }
        }
        //创建一个文件

        try {

            workbook.write(out);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
